package com.cn.demo4.controller;

import com.alibaba.fastjson.JSONObject;
import com.cn.demo4.common.LoadImg;
import com.cn.demo4.common.Tool;
import com.cn.demo4.entity.NewsEntity;
import com.cn.demo4.entity.PageEntity;
import com.cn.demo4.entity.ProductEntity;
import com.cn.demo4.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
public class ProductController {
    @Autowired
    private IProductService iProductService;
    @RequestMapping("/admin/product/findAll")
    @ResponseBody
    public void findAll(HttpServletResponse resp, ProductEntity productEntity, HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        String status = req.getParameter("status");
        status = status == null ? "-1": status;
        String startPrice = req.getParameter("startPrice");
        String endPrice = req.getParameter("endPrice") ;
        productEntity.setStartPrice(Tool.stringToDouble(startPrice));
        productEntity.setEndPrice(Tool.stringToDouble(endPrice));
        productEntity.setStatus(Tool.stringToInt(status));
        PageEntity pageEntity = new PageEntity();
        pageEntity.setCurPage(Tool.toCurPage(req.getParameter("curPage")));
        pageEntity.setPageSize(Tool.toPageSize(req.getParameter("pageSize")));
        productEntity.setPageEntity(pageEntity);
        List<ProductEntity> list = iProductService.findAll(productEntity);
        int count = iProductService.getCount(productEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("count",count);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/admin/product/findById")
    @ResponseBody
    public void findById(HttpServletResponse resp, ProductEntity productEntity, HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        ProductEntity model = iProductService.findById(productEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("model",model);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/adminFile/product/add")
    @ResponseBody
    public void add(HttpServletResponse resp, ProductEntity productEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        LoadImg.loadImg(req);
        productEntity.setName((String) req.getAttribute("name"));//参数对应的是前台ajax中data中的属性名
        productEntity.setMarketPrice(Tool.stringToDouble((String) req.getAttribute("marketPrice")));
        productEntity.setNormalPrice(Tool.stringToDouble((String) req.getAttribute("normalPrice")));
        productEntity.setDescription((String) req.getAttribute("description"));
        productEntity.setImg((String) req.getAttribute("img"));
//        productModel.setNavImg((String) req.getAttribute("navImg"));
//        productModel.setExample((String) req.getAttribute("example"));
        productEntity.setIsShow(Tool.stringToInt((String) req.getAttribute("isShow")));
        productEntity.setHouseName((String) req.getAttribute("houseName"));
        productEntity.setHouseId(Tool.stringToInt((String) req.getAttribute("houseId")));
        productEntity.setCreateTime((String) req.getAttribute("createTime"));
        productEntity.setUpdateTime((String) req.getAttribute("updateTime"));
        productEntity.setStatus(Tool.stringToInt((String) req.getAttribute("status")));
        String result = iProductService.add(productEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/adminFile/product/update")
    @ResponseBody
    public void update(HttpServletResponse resp, ProductEntity productEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        LoadImg.loadImg(req);
        //3.封装数据
        productEntity.setId(Tool.stringToInt((String) req.getAttribute("id")));
        //参数对应的是前台ajax中data中的属性名
        productEntity.setName((String) req.getAttribute("name"));
        productEntity.setMarketPrice(Tool.stringToDouble((String) req.getAttribute("marketPrice")));
        productEntity.setNormalPrice(Tool.stringToDouble((String) req.getAttribute("normalPrice")));
        productEntity.setDescription((String) req.getAttribute("description"));
        productEntity.setImg((String) req.getAttribute("img"));
        productEntity.setIsShow(Tool.stringToInt((String) req.getAttribute("isShow")));
        productEntity.setHouseName((String) req.getAttribute("houseName"));
        productEntity.setHouseId(Tool.stringToInt((String) req.getAttribute("houseId")));
        productEntity.setCreateTime((String) req.getAttribute("createTime"));
        productEntity.setUpdateTime((String) req.getAttribute("updateTime"));
        productEntity.setStatus(Tool.stringToInt((String) req.getAttribute("status")));
        String result = iProductService.update(productEntity);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
    @RequestMapping("/admin/product/del")
    @ResponseBody
    public void del(HttpServletResponse resp, ProductEntity productEntity) throws IOException {
        int result = iProductService.del(productEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("result",result);
        resp.getWriter().println(jsonObject);
    }
//    @RequestMapping("/build")
//    @ResponseBody
//    public void getProductModelListForHome(HttpServletResponse resp, ProductEntity productEntity,HttpServletRequest req) throws IOException {
//        Tool.setEncoding(req,resp);
//        List<ProductEntity> list = iProductService.getProductModelListForHome(productEntity);
//        JSONObject jsonObject = new JSONObject();
//        jsonObject.put("list",list);
//        resp.getWriter().println(jsonObject);
//    }
    @RequestMapping("/admin/product/findByDetailsId")
    @ResponseBody
    public void findByDetailsId(HttpServletResponse resp, ProductEntity productEntity,HttpServletRequest req) throws IOException {
        Tool.setEncoding(req,resp);
        List<ProductEntity> list = iProductService.findByDetailsId(productEntity.getId());
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        resp.getWriter().println(jsonObject);
    }
}
